package com.kuang.test;

import java.util.Arrays;
import java.util.concurrent.locks.ReentrantLock;

public class FairLock extends Thread{

    private ReentrantLock lock;

    public FairLock(boolean isFair){
        lock = new ReentrantLock(isFair);
    }

    @Override
    public void run(){
        try{
            lock.lock();
            System.out.println("⭐线程 "+ Thread.currentThread().getName() + "运行");
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        FairLock fairLock = new FairLock(false);
        Thread[] threads = new Thread[10];
        for (int i = 0;i< 10;i++){
            threads[i] = new Thread(fairLock);
        }
        Arrays.stream(threads).forEach(thread -> thread.start());
    }

}
